package cn.com.chengmandian.bike.rabbitmq;

import cn.com.chengmandian.rabbitmq.constants.RabbitMqConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitMqConfig {

    @Bean
    public MessageConverter jackson2JsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    /**
     * 当前实例接受缓存过期通知
     */
    @Bean
    public Queue localCacheNotifyQueue() {
        return new AnonymousQueue();
    }

    @Bean
    FanoutExchange localCacheNotifyExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl" , 60*1000); // 消息过期时间 60秒
        return new FanoutExchange(RabbitMqConstants.EXCHANGE_LOCAL_CACHE_NOTIFY, true, false, arguments);
    }

    @Bean
    Binding bindingLocalCacheNotify() {
        return BindingBuilder.bind(localCacheNotifyQueue()).to(localCacheNotifyExchange());
    }

    /**
     * 骑行订单指令通知
     * @return
     */
    @Bean
    DirectExchange orderCommandNotifyExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl" , 60*1000 * 10); // 消息过期时间
        return new DirectExchange(RabbitMqConstants.EXCHANGE_RIDE_COMMAND_NOTIFY, true, false, arguments);
    }

    @Bean
    public Queue openLockQueue() {
        return new Queue(RabbitMqConstants.QUEUE_RIDE_COMMAND_OPEN_LOCK, true);
    }

    @Bean
    public Queue stopLockQueue() {
        return new Queue(RabbitMqConstants.QUEUE_RIDE_COMMAND_STOP_LOCK, true);
    }

    @Bean
    public Queue closeLockQueue() {
        return new Queue(RabbitMqConstants.QUEUE_RIDE_COMMAND_CLOSE_LOCK, true);
    }

    @Bean
    Binding bindingOpenLock() {
        return BindingBuilder.bind(openLockQueue()).to(orderCommandNotifyExchange()).with(RabbitMqConstants.ROUTING_RIDE_COMMAND_OPEN_LOCK);
    }

    @Bean
    Binding bindingStopLock() {
        return BindingBuilder.bind(stopLockQueue()).to(orderCommandNotifyExchange()).with(RabbitMqConstants.ROUTING_RIDE_COMMAND_STOP_LOCK);
    }

    @Bean
    Binding bindingCloseLock() {
        return BindingBuilder.bind(closeLockQueue()).to(orderCommandNotifyExchange()).with(RabbitMqConstants.ROUTING_RIDE_COMMAND_CLOSE_LOCK);
    }


    @Bean
    DirectExchange bikePayExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl" , 60*60*1000); // 消息过期时间1小时
        return new DirectExchange(RabbitMqConstants.EXCHANGE_BIKE_PAY, true, false, arguments);
    }

    @Bean
    public Queue walletPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_WALLET, true);
    }

    @Bean
    Binding bindingWalletPay() {
        return BindingBuilder.bind(walletPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_WALLET);
    }

    @Bean
    public Queue depositPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_DEPOSIT, true);
    }

    @Bean
    Binding bindingDepositPay() {
        return BindingBuilder.bind(depositPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_DEPOSIT);
    }

    @Bean
    public Queue cardPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_CARD, true);
    }

    @Bean
    Binding bindingCardPay() {
        return BindingBuilder.bind(cardPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_CARD);
    }

    @Bean
    public Queue depositCardPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_DEPOSIT_CARD, true);
    }

    @Bean
    Binding bindingDepositCardPay() {
        return BindingBuilder.bind(depositCardPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_DEPOSIT_CARD);
    }

    @Bean
    public Queue rideOrderPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_RIDE_ORDER, true);
    }

    @Bean
    Binding bindingRideOrderPay() {
        return BindingBuilder.bind(rideOrderPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_RIDE_ORDER);
    }

    @Bean
    public Queue rentOrderPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_RENT_ORDER, true);
    }

    @Bean
    Binding bindingRentOrderPay() {
        return BindingBuilder.bind(rentOrderPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_RENT_ORDER);
    }

    @Bean
    public Queue rentPenaltyPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_RENT_PENALTY, true);
    }

    @Bean
    Binding bindingRentPenaltyPay() {
        return BindingBuilder.bind(rentPenaltyPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_RENT_PENALTY);
    }

    @Bean
    public Queue memberComboPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_MEMBER_COMBO, true);
    }

    @Bean
    Binding bindingMemberComboPay() {
        return BindingBuilder.bind(memberComboPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_MEMBER_COMBO);
    }

    @Bean
    public Queue ridePenaltyPayQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_PAY_RIDE_PENALTY, true);
    }

    @Bean
    Binding bindingRidePenaltyPay() {
        return BindingBuilder.bind(ridePenaltyPayQueue()).to(bikePayExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_PAY_RIDE_PENALTY);
    }

    @Bean
    DirectExchange bikeRefundExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl" , 60*60*1000); // 消息过期时间1小时
        return new DirectExchange(RabbitMqConstants.EXCHANGE_BIKE_REFUND, true, false, arguments);
    }

    @Bean
    public Queue refundQueue() {
        return new Queue(RabbitMqConstants.QUEUE_BIKE_REFUND, true);
    }

    @Bean
    Binding bindingRefund() {
        return BindingBuilder.bind(refundQueue()).to(bikeRefundExchange()).with(RabbitMqConstants.ROUTING_KEY_BIKE_REFUND);
    }
}
